//
//  ADUTabBar2.m
//  微博OC-01
//
//  Created by lanouhn on 16/6/23.
//  Copyright © 2016年 lanouhn. All rights reserved.
//

#import "ADUTabBar2.h"
#import "ADUTabBarButton.h"


// command + option + shift + 左/右箭头
@interface ADUTabBar2 ()
/**
 *  保存当前选中的按钮
 */
@property (nonatomic,strong) ADUTabBarButton *currentSelectedBtn;
/**
 *  加号按钮
 */
@property (nonatomic,strong) UIButton *plusBtn;
/**
 *  存储所有的选项卡按钮
 */
@property (nonatomic, strong) NSMutableArray *btns;
@end
@implementation ADUTabBar2
// 通过代码创建控件时会调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

// 通过xib/Storboard创建时调用
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{   //1.设置背景颜色
    self.backgroundColor = [UIColor whiteColor];
    //2.创建4个按钮
    
    
    //3.创建加号按钮
    [self addPlushBtn];
}
/**
 *  创建加号按钮
 */
#pragma mark-----创建加号按钮
/**
 *  加号按钮在初始化时候添加, 其他四个选项卡在addItem方法中添加 加号先添加
 */
-(void)addPlushBtn{
    
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    // 4.添加
    [self addSubview:plusBtn];
    
    self.plusBtn = plusBtn;
    
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageW = self.width;
    CGFloat imageH = self.height * 0.6;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    // Y = 按钮的高度 - 图片的高度
    CGFloat titleY = self.height * 0.6;
    CGFloat titleW = self.width;
    CGFloat titleH = self.height - self.height * 0.6;
    return CGRectMake(titleX, titleY, titleW, titleH);
}
-(void)addItem:(UITabBarItem *)item{
    //1.创建按钮
//    UIButton *btn = [[UIButton alloc] init];
      ADUTabBarButton *btn = [[ADUTabBarButton alloc] init];
    //2.设置按钮属性
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn setImage:item.image forState:UIControlStateNormal];
    [btn setImage:item.selectedImage forState:UIControlStateSelected];
    
    
    //3.0添加监听事件
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchDown];
    
    
    //3.添加按钮到view
    [self addSubview:btn];
    
    
    // 4.将按钮添加到数组中
    btn.tag = self.btns.count;// 0 1 2 3 4
    [self.btns addObject:btn]; // 1
    
    //4.设置默认选中按钮
    //默认情况下subViews.count等于0,只要添加了一个按钮那么就代表肯定是第一个 第一个是button
    
    
    //进入这个方法的必定不是加号按钮
    if (self.btns.count == 1) {
        [self btnOnClick:btn];
    }
    
    
    /*
    if (self.subviews.count == 1) {
        [self btnOnClick:btn];
    }
    
    */
   
}

#pragma mark按钮监听事件
-(void)btnOnClick:(ADUTabBarButton *)btn{

    
    //1.切换控制器 tabBarContaroller控制器最清楚有哪些自控制器 点击后需要通知控制器
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedBtnFrom:to:)]) {
        [self.delegate tabBar:self selectedBtnFrom:self.currentSelectedBtn.tag to:btn.tag];
    }

    //2.设置切换按钮的状态
    //取消上一次选中
    self.currentSelectedBtn.selected = NO;
    //选中当前点击的按钮
    btn.selected = YES;
    //记录当前选中的按钮
    self.currentSelectedBtn = btn;
    
    //3.让当前选中的按钮执行动画
    //缩小 ---->放大 --->还原 transfrom
//    [UIView animateWithDuration:0.1 animations:^{
//      //    }];

    
     [UIView animateWithDuration:0.1 animations:^{
         // 缩小
         // sx / sy 如果是1代表不缩放, 如果大于1代表放大 如果小于1代表缩小
          btn.transform = CGAffineTransformMakeScale(0.4, 0.4);

     } completion:^(BOOL finished) {
         [UIView animateWithDuration:0.1 animations:^{
             // 放大
             btn.transform = CGAffineTransformMakeScale(1.4, 1.4);

         } completion:^(BOOL finished) {
             [UIView animateWithDuration:0.1 animations:^{
                 // 还原
                 btn.transform = CGAffineTransformIdentity;
             }];

         }];
    
     }];
    
   
}


#pragma mark设置子控件frame
- (void)layoutSubviews
{
    [super layoutSubviews];
    //设置其他选项看按钮的frame
    NSUInteger index = 0;
    for (UIView *child in self.subviews) {
        if (child.tag != 998) {
            
            
            /**
             *  如果在下边方法中添加 会引起按钮重复点击
             *
             *  @param index <#index description#>
             *
             *  @return <#return value description#>
             */
            //进入这个方法的必定不是加号按钮
//            if (index == 0) {
//                [self btnOnClick:(ADUTabBarButton *)child];
//            }
//            
            
     
            // 2.计算每个按钮的frame
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            // 判断是否是中间一个按钮
            if (index == 2) {
                index++;
                childX = index * childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
    }
    
    //设置加号按钮的frame
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;

}
#pragma mark - 懒加载
- (NSMutableArray *)btns
{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

@end
